import 'package:flutter/material.dart';
import 'package:left_scroll_actions/cupertinoLeftScroll.dart';
import 'package:left_scroll_actions/global/actionListener.dart';
import 'package:left_scroll_actions/left_scroll_actions.dart';
import 'package:provider/provider.dart';
import 'package:useable_app/eventBus/EventBus.dart';
import 'package:useable_app/global/Global.dart';
import 'package:useable_app/utils/alert_utils.dart';
import 'package:useable_app/viewmodel/AccountingAddViewModel.dart';
import '../../base/view.dart';
import '../../viewmodel/AccountingViewModel.dart';

class AccountingAddPage extends StatefulWidget {
  const AccountingAddPage({Key? key}) : super(key: key);

  @override
  _AccountingAddPageState createState() => _AccountingAddPageState();
}

class _AccountingAddPageState extends State<AccountingAddPage> {
  TextEditingController _money = TextEditingController();
  TextEditingController _desc = TextEditingController();

  @override
  void initState() {
    super.initState();
    bus.on("accouting_add_result", (arg) {
      context.read<AccountingViewModel>().accountingHistory();
      setState(() {
        _money.text = "";
        _desc.text = "";
      });
      Navigator.of(context).pop();
    });
  }

  @override
  void dispose() {
    super.dispose();
    // _money.dispose();
    // _desc.dispose();
    bus.off("accouting_add_result");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppbar('新增'),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            children: [
              ListTile(
                title: Text("类型"),
                trailing: Text(Provider.of<AccountingAddViewModel>(context).currentTypeName),
                onTap: _showTypesAlert,
              ),
              Divider(
                height: 1,
              ),
              ListTile(
                title: Text("方式"),
                trailing: Text(
                    Provider.of<AccountingAddViewModel>(context).currentModeName == null ?
                    "" :
                    Provider.of<AccountingAddViewModel>(context).currentModeName
                  // "11"
                ),
                onTap: _accountingType,
              ),
              Divider(
                height: 1,
              ),
              Container(
                padding: EdgeInsets.all(10),
                child: TextField(
                    decoration: InputDecoration(hintText: '请输入金额'),
                    keyboardType: TextInputType.number,
                    textInputAction: TextInputAction.next,
                    controller: _money
                ),
              ),
              Divider(
                height: 1,
              ),
              Container(
                padding: EdgeInsets.all(10),
                child: TextField(
                  decoration: InputDecoration(hintText: '请输入备注'),
                  // keyboardType: TextInputType.number,
                  textInputAction: TextInputAction.done,
                  controller: _desc,
                ),
              ),
              SizedBox(height: 20,),
              Container(
                width: double.infinity,
                child: ElevatedButton(onPressed: _submit, child: Text('提交'))
                ,
              )
            ],
          ),
        ),
      )

    );
  }

  _showTypesAlert() async{
    print(context.read<AccountingAddViewModel>().currentModeName);
    var res = await showObjectAlertDialog(
        context.read<AccountingAddViewModel>().getTypes, '选择类型', 'name'
    );

    context.read<AccountingAddViewModel>().setMode(null);
    context.read<AccountingAddViewModel>().setType(res);
  }

  void _accountingType() async {
    context.read<AccountingAddViewModel>().getModeAndAlert();
  }

  void _submit() async {
    context.read<AccountingAddViewModel>().insert(_money.text, _desc.text);
  }
}
